/*
* Nom de classe : NavigationFram
*
* Description   : fram de navigation
*
* Version       : 0.0
*
* Date          : 31/01/2014
*
* Update     	: Julian
*/
package org.exia.minisig.swing.view.navigation;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import org.exia.minisig.application.LogFile;
import org.exia.minisig.application.Utils;
import org.exia.minisig.model.object.DescriptibleElement;
import org.exia.minisig.model.object.Interest;
import org.exia.minisig.model.object.Place;
import org.exia.minisig.services.InterestManager;
import org.exia.minisig.services.ItineraryManager;
import org.exia.minisig.swing.controller.GUIManager;
import org.exia.minisig.swing.controller.HomeFramMode;
import org.exia.minisig.swing.view.common.AddPanel;
import org.exia.minisig.swing.view.common.InformationPanel;
import org.exia.minisig.swing.view.common.MiniSIGSwing;
import org.exia.minisig.swing.view.common.ResultList;
import org.exia.minisig.swing.view.common.SearchPanel;

/**
* NavigationFram - Fram de navigation
* @version0.0
* @date 31/01/2013
*/
@SuppressWarnings("serial")
public class NavigationFram extends JFrame implements MiniSIGSwing {
	//VAR
	private MapComponent map;
	private ItineraryManager serviceItinerary;
	private InterestManager serviceInterest;
	private DescriptibleElement selectedElement;
	private ArrayList<DescriptibleElement> listeInterest;
	public DescriptibleElement getSelectedElement() {
		return selectedElement;
	}
	private ResultList results;
	public Place getPlace() {
		return place;
	}
	public void setPlace(Place place) {
		this.place = place;
	}
	public boolean isNavigationMode() {
		return navigationMode;
	}
	public void setNavigationMode(boolean navigationMode) {
		this.navigationMode = navigationMode;
	}
	private Place place;
	private JPanel panelRight;
	private JPanel panelLeft;
	private JPanel panelContext;
	private SearchPanel search;
	private JButton btnSwap; 
	private boolean navigationMode;
	private boolean searchMode;
	private boolean addMode;
	private boolean editMode;
	public boolean isEditMode() {
		return editMode;
	}
	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
	}
	private InterestAddPanel panelAddInterest;
	//GET & SET
	public MapComponent getMap() {
		return map;
	}
	public void setMap(MapComponent map) {
		this.map = map;
	}
	public InterestManager getServiceInterest() {
		return serviceInterest;
	}
	public void setServiceInterest(InterestManager serviceInterest) {
		this.serviceInterest = serviceInterest;
	}
	public ItineraryManager getServiceItinerary() {
		return serviceItinerary;
	}
	public void setServiceItinerary(ItineraryManager serviceItinerary) {
		this.serviceItinerary = serviceItinerary;
	}
	public boolean isSearchMode() {
		return searchMode;
	}
	public void setSearchMode(boolean searchMode) {
		this.searchMode = searchMode;
	}
	public boolean isAddMode() {
		return addMode;
	}
	public void setAddMode(boolean addMode) {
		this.addMode = addMode;
	}
	//CONSTRUCTOR
	@SuppressWarnings("unused")
	private NavigationFram(){}
	public NavigationFram(Place _map){
		initializeVar(_map);
	}
	public void initializeVar(Place _map){
		this.serviceInterest = new InterestManager();
		this.serviceItinerary = new ItineraryManager();
		this.panelContext = new JPanel();
		this.place = _map;
		listeInterest = new ArrayList<>();
		loadInterest();
		/*------------------------------------*/
		this.search = new SearchPanel(listeInterest, this);
		/*------------------------------------*/
		this.results = new ResultList(this, listeInterest);
		/*------------------------------------*/
		this.panelRight = new JPanel();
		this.panelLeft = new JPanel(null){
            //OVERRIDE 
            @Override
            public void paintComponent(Graphics g){
                try {
                        Image background = ImageIO.read(this.getClass().getResource("/images/navigation/background.jpg"));
                        for (int i = 0; i < (this.getWidth() / background.getWidth(null)) +1; i++) {
                                for (int j = 0; j < this.getHeight() / background.getHeight(null) +1; j++) {
                                        g.drawImage(background, i*19, j*19, null);
                                }
                        }
                } catch (IOException e) {
                        LogFile.setEntry("Erreur chargement fond de map \n" + e.getCause());
                }
            }};
		//
        String swapText = "Mode Recherche";
		if (addMode){
			swapText = "Mode Information";
		}
		btnSwap = new JButton(swapText);
		btnSwap.addActionListener(new ActionListener() {                
			 @Override
			 public void actionPerformed(ActionEvent arg0) {
				 addMode = false;
				 if (searchMode){
				    btnSwap.setText("Mode recherche");
		      		searchMode = false;
			     } else {
			    	 searchMode = true;
			    	 btnSwap.setText("Mode information");
			     }
				 fillRightPanel();
			 }
	  	});
        //
        fill();
        fillRightPanel();
	}
	//METHODE
	public void fill(){
		/*-------------------------PANEL PRINCIPAUX--------------------------------*/
		this.getContentPane().removeAll();
		this.getContentPane().setLayout(new GridBagLayout());
		GridBagConstraints gc = new GridBagConstraints();
		gc.fill=GridBagConstraints.BOTH;  
        //
		gc.weightx=4;
		gc.weighty=1;
		//
		gc.gridheight = 1;
		gc.weightx = 9;
		gc.gridx=0;
		gc.gridy=0;
		this.getContentPane().add(panelLeft, gc);
		//
		gc.weightx = 1;
		gc.gridheight = 1;
		gc.gridx=1;
		gc.gridy=0;
		this.getContentPane().add(panelRight, gc);
		/*-----------------------PANEL LEFT/MAP--------------------------------------*/
		//Button Zomm +/-
        JButton zoomUp = new JButton();
        zoomUp.setSize(30, 30);
        zoomUp.setLocation(5, 5);
        zoomUp.setIcon(new ImageIcon(this.getClass().getResource("/images/navigation/zoommore.png")));
        zoomUp.setFocusPainted(false);
        this.panelLeft.add(zoomUp);
        JButton zoomDown = new JButton();
        zoomDown.setSize(30, 30);
        zoomDown.setLocation(5, 35);
        zoomDown.setIcon(new ImageIcon(this.getClass().getResource("/images/navigation/zoomless.png")));
        zoomDown.setFocusPainted(false);
        this.panelLeft.add(zoomDown);
        //
        map = new MapComponent(this, this.place, panelLeft, zoomUp, zoomDown);
        map.setSize(this.place.getMap().getWidth(null), this.place.getMap().getHeight(null));
        this.panelLeft.add(map);
		panelLeft.add(map);
  		this.revalidate();
	}
	public JPanel getPanelLeft() {
		return panelLeft;
	}
	public void setPanelLeft(JPanel panelLeft) {
		this.panelLeft = panelLeft;
	}
	public void loadInterest(){
		this.listeInterest = new ArrayList<>();
		for (Interest element : place.getInterests()) {
			this.listeInterest.add((DescriptibleElement)element);
		}
	}
	public void fillRightPanel(){
		/*-----------------------PANEL RIGHT--------------------------------------*/
		this.panelRight.removeAll();
		this.panelRight.setLayout(new GridBagLayout());
		GridBagConstraints gc2 = new GridBagConstraints();
		gc2.fill=GridBagConstraints.BOTH;  
        //
		gc2.weightx=4;
		gc2.weighty=1;
		//-----BTN HOME
		gc2.weighty = 0.1;
		gc2.gridwidth = 2;
		gc2.gridx=0;
		gc2.gridy=0;
		JButton btnHome = new JButton("Retour Accueil");
	  	btnHome.addActionListener(new ActionListener() {                
			 @Override
			 public void actionPerformed(ActionEvent arg0) {
			         GUIManager.getInstance().runHome();
			 }
	  	});
	  	this.panelRight.add(btnHome, gc2);
	  	//-----BTN Swap & BTN add POI
	  	//
	  	gc2.gridwidth = 1;
	  	gc2.gridx=0;
		gc2.gridy=1;
	  	this.panelRight.add(btnSwap, gc2);
	  	//
	  	gc2.gridx=1;
		gc2.gridy=1;
		JButton btnAdd = new JButton("Ajouter POI");
		btnAdd.addActionListener(new ActionListener() {                
			 @Override
			 public void actionPerformed(ActionEvent arg0) {
			      	addMode = true;
			      	navigationMode = false;
			      	fillRightPanel();
			 }
	  	});
	  	this.panelRight.add(btnAdd, gc2);
	  	//-----Panel Search / Ajout / Info
	  	gc2.gridwidth = 2;
	  	gc2.gridx=0;
		gc2.gridy=2;
		gc2.weighty = 10;
		///////////////////////////////
		this.panelContext.removeAll();
		this.panelContext.setLayout(new GridBagLayout());
		GridBagConstraints gc3 = new GridBagConstraints();
		gc3.fill=GridBagConstraints.BOTH;  
        //
		gc3.weightx=4;
		gc3.weighty=1;
		//
		if (this.navigationMode && selectedElement != null){
			ItineraryNavigation nav = new ItineraryNavigation(selectedElement, this);
			this.panelRight.add(nav, gc2);
		} else if (this.editMode){
			InterestEditPanel panel = new InterestEditPanel(selectedElement, this);
  			this.panelRight.add(panel, gc2);
  		} else if (this.addMode){
  			panelAddInterest = new InterestAddPanel("", this);
  			this.panelRight.add(panelAddInterest, gc2);
  		} else if (this.searchMode){
  			//Search mode OK
  		  	gc3.gridx=0;
  			gc3.gridy=0;
  			gc3.weighty = 0.3;
  			this.panelContext.add(search, gc3);
  			gc3.weighty = 10;
  			gc3.gridx=0;
  			gc3.gridy=1;
  			this.results = new ResultList(this, listeInterest);
  			this.panelContext.add(results, gc3);
  		} else if (selectedElement != null){
  			this.panelRight.add(new InterestInfoPanel(selectedElement, this), gc2);
  		} else {
  			JPanel test = new JPanel();
  			this.panelRight.add(test, gc2);
  		}
  		//
  		this.panelRight.add(panelContext, gc2);
  		this.revalidate();
	}
	public void mapIsWaitingClic(boolean b ){
		map.setWaitClic(b);
	}
	private Point positionClicked;
	public Point getPositionClicked(){
		return positionClicked;
	}
	public void reloadPositionDisplay(Point p){
		panelAddInterest.changeLabelPosition(p);
		positionClicked = p;
	}
	
	//OVERRIDE
	@Override
	public Dimension getPreferredSize(){
		return new Dimension(840, 480);
	}
	@Override
	public void reload() {
		fillRightPanel();	
	}
	@Override
	public void setSelectedElement(DescriptibleElement _selectedElement) {
		this.searchMode = false;
		this.addMode  =false;
		this.selectedElement = _selectedElement;
		this.reload();
	}
	@Override
	public HomeFramMode getMode() {
		return HomeFramMode.INTEREST;
	}
	public void AddMode(boolean b) {
		this.addMode = b;
	}
	public void EditMode(boolean b) {
		if (b){
			this.navigationMode = false;
		}
		this.editMode = b;
		
	}
}

